<template>
  <div style="padding: 10px">
    <div style="padding: 10px 15px">
      <tableTurn :tableData="getData" :args="args" ref="tableRef">
        <a-table-column :width="80" align="center">
          <template #title> 序号 </template>
          <template #cell="{ rowIndex }">
            {{ rowIndex + 1 }}
          </template>
        </a-table-column>
        <a-table-column
          v-for="item in columns"
          :key="item.code"
          :title="item.name"
          :data-index="item.code"
          :width="150"
          align="center"
        >
          <template v-if="item.code === 'applyDetail'" #cell="{ record }">
            <a-typography-text
              class="Detail-item"
              type="primary"
              @click="See(record.product)"
            >
              查看
            </a-typography-text>
          </template>

          <template v-else-if="item.code === 'applyCause'" #cell="{ record }">
            <a-typography-text
              class="Detail-item"
              type="primary"
              @click="view(record.cause)"
            >
              查看
            </a-typography-text>
          </template>

          <template v-else-if="item.code === 'entryDate'" #cell="{ record }">
            {{ dayjs(record.entryDate).format('YYYY-MM-DD HH:mm:ss') }}
          </template>

          <template v-else-if="item.code === 'createdAt'" #cell="{ record }">
            {{ dayjs(record.createdAt).format('YYYY-MM-DD') }}
          </template>

          <template v-else-if="item.code === 'priority'" #cell="{ record }">
            <a-typography-text
              v-if="record.priority === 1"
              style="color: #999999"
            >
              低
            </a-typography-text>
            <a-typography-text v-if="record.priority === 2" type="primary">
              中
            </a-typography-text>
            <a-typography-text v-if="record.priority === 3" type="warning">
              高
            </a-typography-text>
            <a-typography-text v-if="record.priority === 4" type="danger">
              紧急
            </a-typography-text>
          </template>

          <template v-else-if="item.code === 'detailed'" #cell="{ record }">
            <a-button type="text" @click="viewDetails(record.id)"
              >查看明细</a-button
            >
          </template>
        </a-table-column>
        <a-table-column :width="200" fixed="right" title="状态" align="center">
          <template #cell="{ record }">
            <a-typography-text type="primary" v-if="record.state === 3">
              上架中
            </a-typography-text>
            <a-typography-text type="success" v-if="record.state === 4">
              上架完毕
            </a-typography-text>
          </template>
        </a-table-column>
      </tableTurn>
    </div>
  </div>

  <ConfirmReceipt
    v-if="ReceiptId"
    v-model:visible="modelVisible"
    :receiptId="ReceiptId"
  />
</template>

<script lang="ts" setup>
  import {
    reactive,
    ref,
    computed,
    onMounted,
    defineAsyncComponent,
  } from 'vue';
  import { useRouter, useRoute } from 'vue-router';
  import dayjs from 'dayjs';
  import {
    GetWarehouseEntryAll,
    GetHeaderAll,
    putHeader,
    delWarehouseEntry,
    putWarehouseEntryState,
    getStorageAll,
  } from '@/api/inventory-management/warehousing-management/receiving-management/index';
  import { GetGroundingAll } from '@/api/inventory-management/warehousing-management/purchase-receipt/index';

  const changeMsg = ref('1');
  const router = useRouter();
  const route = useRoute();
  const id = Number(route.query.id);
  const listType = ref(2);
  const modelVisible = ref(false);
  const formHeaderSettingVisible = ref(false);
  let tableRef: any = ref(); // 储存table的ref
  let currSelectVal = ref(); // 储存当前下拉选择框的值
  let currSelectSearch = ref(''); // 储存下拉选择框对应的搜索框的值
  const modelCauseVisible = ref(false);
  const userState = ref();
  const beginTime = ref();
  const endTime = ref();
  const ownList = ref();
  const ReceiptId = ref();
  const saveUserId = ref<any>(
    JSON.parse(sessionStorage.getItem('loginInfo') as any)
  );
  const getData = async (page: any) => {
    try {
      if (changeMsg.value === '1') {
        let res = await getStorageAll({
          ...page,
          entryWarehouseId: id,
        });
        if (res.content === null || res.content === undefined) {
          res.content = [];
        }

        res.content.map((item: any) => {
          item.show = false;
          return item;
        });
        return Promise.resolve(res);
      } else {
        let res = await getStorageAll({
          ...page,
          entryWarehouseId: id,
        });
        return Promise.resolve(res);
      }
    } catch (err: unknown) {
      console.log(err);
    }
  };

  const columns = [
    {
      code: 'number',
      isDefault: 1,
      name: '采购入库单号',
    },
    {
      code: 'entryWarehouseNumber',
      isDefault: 1,
      name: '关联收货单号',
    },
    {
      code: 'supplierName',
      isDefault: 1,
      name: '供应商名称',
    },
    {
      code: 'purchaseStaffName',
      isDefault: 1,
      name: '采购员',
    },
    {
      code: 'location',
      isDefault: 1,
      name: '暂存区域',
    },
    {
      code: 'entryStaffName',
      isDefault: 1,
      name: '收货人',
    },
    {
      code: 'inspectionStaffName',
      isDefault: 1,
      name: '质检员',
    },
    {
      code: 'warehouseStaffName',
      isDefault: 1,
      name: '仓管员',
    },
  ];

  const showFormHeaderSetting = () => {
    formHeaderSettingVisible.value = true;
  };

  const onChange = (value: any) => {
    if (value !== undefined) {
      beginTime.value = value[0];
      endTime.value = value[1];
      tableRef.value.loadData();
    } else {
      tableRef.value.loadData();
    }
  };

  const goDelivery = async (id: number) => {
    modelVisible.value = true;
    ReceiptId.value = id;
  };

  const changeTabs = (e: any) => {
    changeMsg.value = e;
    if (e === '1') {
      listType.value = 2;
    } else {
      listType.value = 3;
    }
    tableRef.value.loadData();
  };
</script>

<style scoped lang="less">
  // .content-head {
  // }
  .search-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }

  :deep(.arco-space-item) {
    margin-top: 10px;
  }
  .Detail-item {
    cursor: pointer;
  }

  :deep(.arco-tabs-content) {
    padding-top: 0px;
  }
</style>
